﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HotelApp
{
    public static class HotelAppOM
    {
        #region AdminInfo

        public static void CreateAdminInfo(AdminInfo obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("CreateAdminInfo: Invalid input parameter");

            if (obj.CheckData(true).Count > 0)
                throw new Exception("CreateAdminInfo: AdminInfo object is invalid");

            obj.CreateDate = DateTime.Now;
            AdminInfoDAO.CreateAdminInfo(obj);
            LogUtil.LogEvent(obj.UserCode, EventAction.CreateAdminInfo, oper, obj.ToString());
        }

        public static void UpdateAdminInfo(AdminInfo obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("UpdateAdminInfo: Invalid input parameter");

            AdminInfo objOld = AdminInfoDAO.GetAdminInfo(obj.UserCode);
            if (objOld == null)
                throw new Exception("UpdateAdminInfo: Can not find existing object");

            //Find difference
            IDictionary<string, string> dicDiff = obj.FindDifference(objOld);
            if (dicDiff.Count > 0)
            {
                AdminInfoDAO.UpdateAdminInfo(obj);
                LogUtil.LogEvent(obj.UserCode, EventAction.UpdateAdminInfo, oper, dicDiff);
            }
        }

        public static void DeleteAdminInfo(string userCode, string oper)
        {
            if (string.IsNullOrEmpty(userCode) || string.IsNullOrEmpty(oper))
                throw new Exception("DeleteAdminInfo: Invalid input parameter");

            AdminInfo objOld = AdminInfoDAO.GetAdminInfo(userCode);
            if (objOld == null)
                throw new Exception("DeleteAdminInfo: Can not find existing object");

            AdminInfoDAO.DeleteAdminInfo(userCode);
            LogUtil.LogEvent(userCode, EventAction.DeleteAdminInfo, oper, objOld.ToString());
        }

        public static void LoginSystem(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                throw new Exception("LoginSystem: Invalid input parameter");

            AdminInfo obj = AdminInfoDAO.LoginSystem(userName, password);
            if (obj == null)
                throw new Exception("LoginSystem: Can not find existing object");

            LogUtil.LogEvent(obj.UserCode, EventAction.LoginSystem, obj.UserName, "Login system at " + DateTime.Now);
        }

        #endregion

        #region NewsCategory

        public static void CreateNewsCategory(NewsCategory obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("CreateNewsCategory: Invalid input parameter");

            if (obj.CheckData(true).Count > 0)
                throw new Exception("CreateNewsCategory: NewsCategory object is invalid");

            obj.CreateDate = DateTime.Now;
            NewsCategoryDAO.CreateCategory(obj);
            LogUtil.LogEvent(obj.CategoryCode, EventAction.CreateNewsCategory, oper, obj.ToString());
        }

        public static void UpdateNewsCategory(NewsCategory obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("UpdateNewsCategory: Invalid input parameter");

            NewsCategory objOld = NewsCategoryDAO.GetCategory(obj.CategoryCode);
            if (objOld == null)
                throw new Exception("UpdateNewsCategory: Can not find existing object");

            //Find difference
            IDictionary<string, string> dicDiff = obj.FindDifference(objOld);
            if (dicDiff.Count > 0)
            {
                NewsCategoryDAO.UpdateCategory(obj);
                LogUtil.LogEvent(obj.CategoryCode, EventAction.UpdateNewsCategory, oper, dicDiff);
            }
        }

        public static void DeleteNewsCategory(string categoryCode, string oper)
        {
            if (string.IsNullOrEmpty(categoryCode) || string.IsNullOrEmpty(oper))
                throw new Exception("DeleteNewsCategory: Invalid input parameter");

            NewsCategory objOld = NewsCategoryDAO.GetCategory(categoryCode);
            if (objOld == null)
                throw new Exception("DeleteNewsCategory: Can not find existing object");

            NewsCategoryDAO.DeleteCategory(categoryCode);
            LogUtil.LogEvent(categoryCode, EventAction.DeleteNewsCategory, oper, objOld.ToString());
        }

        #endregion

        #region NewsInfo

        public static void CreateNewsInfo(NewsInfo obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("CreateNewsInfo: Invalid input parameter");

            if (obj.CheckData(true).Count > 0)
                throw new Exception("CreateNewsInfo: NewsInfo object is invalid");

            obj.CreateDate = DateTime.Now;
            NewsInfoDAO.CreateNewsInfo(obj);
            LogUtil.LogEvent(obj.NewsCode, EventAction.CreateNewsInfo, oper, obj.ToString());
        }

        public static void UpdateNewsInfo(NewsInfo obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("UpdateNewsInfo: Invalid input parameter");

            NewsInfo objOld = NewsInfoDAO.GetNewsInfo(obj.NewsCode);
            if (objOld == null)
                throw new Exception("UpdateNewsInfo: Can not find existing object");

            //Find difference
            IDictionary<string, string> dicDiff = obj.FindDifference(objOld);
            if (dicDiff.Count > 0)
            {
                NewsInfoDAO.UpdateNewsInfo(obj);
                LogUtil.LogEvent(obj.NewsCode, EventAction.UpdateNewsInfo, oper, dicDiff);
            }
        }

        public static void DeleteNewsInfo(string newsCode, string oper)
        {
            if (string.IsNullOrEmpty(newsCode) || string.IsNullOrEmpty(oper))
                throw new Exception("DeleteNewsInfo: Invalid input parameter");

            NewsInfo objOld = NewsInfoDAO.GetNewsInfo(newsCode);
            if (objOld == null)
                throw new Exception("DeleteNewsInfo: Can not find existing object");

            NewsInfoDAO.DeleteNewsInfo(newsCode);
            LogUtil.LogEvent(newsCode, EventAction.DeleteNewsInfo, oper, objOld.ToString());
        }

        #endregion

        #region ImageCategory

        public static void CreateImageCategory(ImageCategory obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("CreateImageCategory: Invalid input parameter");

            if (obj.CheckData(true).Count > 0)
                throw new Exception("CreateImageCategory: ImageCategory object is invalid");

            obj.CreateDate = DateTime.Now;
            ImageCategoryDAO.CreateCategory(obj);
            LogUtil.LogEvent(obj.CategoryCode, EventAction.CreateImageCategory, oper, obj.ToString());
        }

        public static void UpdateImageCategory(ImageCategory obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("UpdateImageCategory: Invalid input parameter");

            ImageCategory objOld = ImageCategoryDAO.GetCategory(obj.CategoryCode);
            if (objOld == null)
                throw new Exception("UpdateImageCategory: Can not find existing object");

            //Find difference
            IDictionary<string, string> dicDiff = obj.FindDifference(objOld);
            if (dicDiff.Count > 0)
            {
                ImageCategoryDAO.UpdateCategory(obj);
                LogUtil.LogEvent(obj.CategoryCode, EventAction.UpdateImageCategory, oper, dicDiff);
            }
        }

        public static void DeleteImageCategory(string categoryCode, string oper)
        {
            if (string.IsNullOrEmpty(categoryCode) || string.IsNullOrEmpty(oper))
                throw new Exception("DeleteImageCategory: Invalid input parameter");

            ImageCategory objOld = ImageCategoryDAO.GetCategory(categoryCode);
            if (objOld == null)
                throw new Exception("DeleteImageCategory: Can not find existing object");

            ImageCategoryDAO.DeleteCategory(categoryCode);
            LogUtil.LogEvent(categoryCode, EventAction.DeleteImageCategory, oper, objOld.ToString());
        }

        #endregion

        #region ImageInfo

        public static void CreateImageInfo(ImageInfo obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("CreateImageInfo: Invalid input parameter");

            if (obj.CheckData(true).Count > 0)
                throw new Exception("CreateImageInfo: ImageInfo object is invalid");

            obj.CreateDate = DateTime.Now;
            ImageInfoDAO.CreateImageInfo(obj);
            LogUtil.LogEvent(obj.ImageCode, EventAction.CreateImageInfo, oper, obj.ToString());
        }

        public static void UpdateImageInfo(ImageInfo obj, string oper)
        {
            if (obj == null || String.IsNullOrEmpty(oper))
                throw new Exception("UpdateImageInfo: Invalid input parameter");

            ImageInfo objOld = ImageInfoDAO.GetImageInfo(obj.ImageCode);
            if (objOld == null)
                throw new Exception("UpdateImageInfo: Can not find existing object");

            //Find difference
            IDictionary<string, string> dicDiff = obj.FindDifference(objOld);
            if (dicDiff.Count > 0)
            {
                ImageInfoDAO.UpdateImageInfo(obj);
                LogUtil.LogEvent(obj.ImageCode, EventAction.UpdateImageInfo, oper, dicDiff);
            }
        }

        public static void DeleteImageInfo(string imageCode, string oper)
        {
            if (string.IsNullOrEmpty(imageCode) || string.IsNullOrEmpty(oper))
                throw new Exception("DeleteImageInfo: Invalid input parameter");

            ImageInfo objOld = ImageInfoDAO.GetImageInfo(imageCode);
            if (objOld == null)
                throw new Exception("DeleteImageInfo: Can not find existing object");

            ImageInfoDAO.DeleteImageInfo(imageCode);
            LogUtil.LogEvent(imageCode, EventAction.DeleteImageInfo, oper, objOld.ToString());
        }
        
        #endregion
    }
}
